package com.yicong.ants.ui.coin;


import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.cchao.simplelib.core.Router;
import com.cchao.simplelib.core.RxHelper;
import com.cchao.simplelib.core.UiHelper;
import com.cchao.simplelib.ui.fragment.SimpleLazyFragment;
import com.yicong.ants.R;
import com.yicong.ants.activity.DoubleConfirmActivity;
import com.yicong.ants.bean.coin.CoinInfo;
import com.yicong.ants.bean.coin.TransInfo;
import com.yicong.ants.databinding.FragmentTickentBinding;
import com.yicong.ants.manager.BusinessHelper;
import com.yicong.ants.manager.business.Dialogs;
import com.yicong.ants.manager.config.ZzConfigs;
import com.yicong.ants.manager.http.RetrofitHelper;
import com.yicong.ants.ui.me.PayPwdSettingActivity;
import com.zx.sdk.util.ToastHelper;

public class TicketAccountFragment extends SimpleLazyFragment<FragmentTickentBinding> implements View.OnClickListener {

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_tickent;
    }

    CoinInfo mData;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mDataBind.setClick(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        onLoadData();
    }

    @Override
    protected void onLoadData() {
        showProgress();
        addSubscribe(RetrofitHelper.getApis().coinInfo()
                .compose(RxHelper.toMain())
                .subscribe(respBean -> {
                    hideProgress();
                    if (respBean.isCodeFail()) {
                        showToast(respBean.getMsg());
                        return;
                    }
                    mData = respBean.getData();
                    updateInfo();
                }, RxHelper.getHideProgressConsumer(this)));
        refreshRateInfo();
    }

    private void updateInfo() {
        mDataBind.setBean(mData);
        if (BusinessHelper.mDashBoard != null) {
            mDataBind.platformSubsidyNow.setText(BusinessHelper.mDashBoard.getTax_total());
        }
    }

    @Override
    public void onFirstUserVisible() {

    }

    private void refreshRateInfo() {
        showProgress();
        addSubscribe(RetrofitHelper.getApis().rateInfo()
                .compose(RxHelper.toMain())
                .subscribe(respBean -> {
                    hideProgress();
                    if (respBean.isCodeFail()) {
                        showToast(respBean.getMsg());
                        return;
                    }
                    mDataBind.rateTxt.setText("(" + respBean.getData().get("coinToUnit") + ")");
                }, RxHelper.getHideProgressConsumer(this)));
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.coin_field) {
            Router.turnTo(mContext, CoinRecordActivity.class).putExtra("total", mData.getCoin_total()).start();
        } else if (id == R.id.stock_field) {
            Router.turnTo(mContext, StockRecordActivity.class).putExtra("total", mData.getStock()).start();
        } else if (id == R.id.points_field) {
            ZzConfigs.toWebActivityByUrl(mContext, "unit_record", "?score=" + mData.getUnit());
        } else if (id == R.id.contribution_field) {
            Router.turnTo(mContext, ContributionRecordActivity.class).start();
        } else if (id == R.id.trans_in || id == R.id.trans_out) {
            if (mData == null) {
                ToastHelper.showToast("数据错误，请刷新重试");
                return;
            }
            if (mData.getHave_pay_password() == 0) {
                UiHelper.showConfirmActionDialog(mContext, "您还没有设置支付密码，是否前往设置"
                        , (dialog, which) -> {
                            //
                            Router.turnTo(mContext, PayPwdSettingActivity.class).start();
                            requireActivity().finish();
                        });
                return;
            }
            int type = v.getId() == R.id.trans_in ? 10 : 20;
            Dialogs.showStockTran(mContext, TransInfo.instance(mData), type, (amount, payPwd) -> {
                //
                BusinessHelper.onStockTrans((UserCoinActivity) requireActivity(), type, amount, payPwd, () -> {
                    //
                    onLoadData();
                });
            });
        }  else if (id == R.id.contribution_rule) {
            ZzConfigs.toWebActivityByUrlNotLogin(mContext, "ant_rule_contribution");
        } else if (id == R.id.subsidy_rule) {
            ZzConfigs.toWebActivityByUrlNotLogin(mContext, "ant_rule_subsidy");
        } else if (id == R.id.trans_unit) {
            ZzConfigs.toWebActivityByUrlNotLogin(mContext, "transfer_unit");
        } else if (id == R.id.rate_refresh) {
            refreshRateInfo();
        } else if (id == R.id.layout_tips) {
            Router.turnTo(mContext, DoubleConfirmActivity.class).putExtra("status", 0).start();
        }
    }
}
